package com.bong.panpan.bongcharttools.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bong.panpan.bongcharttools.R;
import com.bong.panpan.bongcharttools.ble.BleInfo;

import java.util.ArrayList;

/**
 * Created by panpan on 2017/9/25.
 */

public class DeviceLinkedListAdapter extends BaseAdapter {
    private Context context;
    private View.OnClickListener listener;
    private ArrayList<BleInfo> listData = new ArrayList<>();

    public DeviceLinkedListAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public BleInfo getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item_device2, parent, false);
            holder.image = (ImageView) convertView.findViewById(R.id.image);
            holder.name = (TextView) convertView.findViewById(R.id.name);
            holder.addr = (TextView) convertView.findViewById(R.id.mac);
            holder.rssi = (TextView) convertView.findViewById(R.id.rssi);
            holder.linkBtn = (Button) convertView.findViewById(R.id.link_btn);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        BleInfo info = listData.get(position);
        holder.linkBtn.setId(position);
        switch (info.state) {
            case DEVICE_STATE_LINKING:
                holder.linkBtn.setText("正在连接");
                break;
            case DEVICE_STATE_LINKED:
                holder.linkBtn.setText("断开连接");
                break;
            case DEVICE_STATE_DISLINK:
                holder.linkBtn.setText("连接");
                break;
            default:
                holder.linkBtn.setText("请稍后");
                break;
        }
        if (listener != null)
            holder.linkBtn.setOnClickListener(listener);
        if (info.name == null)
            holder.name.setText("N/A");
        else
            holder.name.setText(info.name);
        if (info.type == 0)
            holder.image.setImageResource(R.mipmap.bluetooth);
        else
            holder.image.setImageResource(R.mipmap.heart_icon);
        if(info.isNotify)
            holder.image.setBackgroundResource(R.drawable.layout_back2);
        else
            holder.image.setBackgroundResource(R.drawable.layout_back);
        holder.image.setId(position);
        holder.image.setOnClickListener(listener);
        holder.addr.setText(info.mac);
        holder.rssi.setText(info.rssi + "dBm");
        return convertView;
    }

    public void setClickListener(View.OnClickListener listener) {
        this.listener = listener;
    }

    public class ViewHolder {
        ImageView image;
        TextView name, addr, rssi;
        Button linkBtn;
    }

    public synchronized void addData(BleInfo info) {
        listData.add(info);
    }
    public synchronized int indexOfData(BleInfo info) {
        return listData.indexOf(info);
    }
    private synchronized void remove(int position) {
        if (position < 0 || position > listData.size() - 1)
            return;
        listData.remove(position);
    }

    public synchronized void removeData(int... positions) {
        for (int i = 0; i < positions.length; i++) {
            remove(positions[i]);
        }
    }

    public synchronized void clear() {
        listData.clear();
    }
}
